package net.lizhongxian.interview.hunktimes.entity;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 订单类
 */
public class Order {

	/**
	 * 所属商店
	 */
	private final Shop shop;

	/**
	 * 商品列表，key-商品 value-数量
	 */
	private final Map<Goods, BigDecimal> goodsList;

	/**
	 * 优惠券列表
	 */
	private final List<Coupon> couponList;

	public Order(Shop shop) {
		this.shop = shop;
		this.goodsList = new HashMap<>();
		this.couponList = new ArrayList<>();
	}

	/**
	 * 添加商品
	 *
	 * @param goods 商品
	 * @param count 数量
	 * @return true-成功 false-失败
	 */
	public boolean addGoods(Goods goods, BigDecimal count) {
		if (!shop.contains(goods)) {
			System.out.println("商品不存在");
			return false;
		}
		if (this.goodsList.containsKey(goods)) {
			this.goodsList.put(goods, this.goodsList.get(goods).add(count));
			System.out.println("订单添加商品成功，商品列表：" + goodsList);
			return true;
		}
		this.goodsList.put(goods, count);
		System.out.println("订单添加商品成功，商品列表：" + goodsList);
		return true;
	}

	/**
	 * 添加优惠券
	 *
	 * @param coupon 优惠券
	 * @return true-成功 false-失败
	 */
	public boolean addCoupon(Coupon coupon) {
		if (coupon.isExclusive()) {
			this.couponList.clear();
		}
		this.couponList.add(coupon);
		System.out.println("订单添加优惠券成功，优惠券列表：" + couponList);
		return true;
	}

	public Map<Goods, BigDecimal> getGoodsList() {
		return goodsList;
	}

	public List<Coupon> getCouponList() {
		return couponList;
	}
}
